package com.lsh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.dao.QuestionBasicDao;
import com.lsh.dao.QuestionItemDao;
import com.lsh.entity.QueryPageBean;
import com.lsh.pojo.Question;
import com.lsh.pojo.QuestionItem;
import com.lsh.pojo.Tag;
import com.lsh.service.QuestionBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionBasicServiceImpl implements QuestionBasicService {
    @Autowired
    private QuestionBasicDao questionBasicDao;
    @Autowired
    private QuestionItemDao questionItemDao;

    @Override
    public PageInfo<Question> findQuestionBasicByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Question> questionList = questionBasicDao.findQuestionBasicByPage(queryPageBean);
        PageInfo<Question> questionPageInfo = new PageInfo<>(questionList);
        return questionPageInfo;
    }
//    useGeneratedKeys="true" keyProperty="id"
    @Override
    public void addQuestion(Question question) {
        System.out.println(question);
        //先对t_question表相关的数据存储到t_question表
        questionBasicDao.addQuestion(question);
        //判断是否有选项信息，如果有则为选择题，把选项放入t_question_item表
        List<QuestionItem> questionItemList = question.getQuestionItemList();
        if(questionItemList != null && questionItemList.size() > 0){
            //有选项
            for (QuestionItem questionItem : questionItemList) {
                questionItem.setQuestionId(question.getId());
                questionItemDao.addQuestionItem(questionItem);
            }
        }
        //将题目和标签的关联信息，存储到tr_question_tag表中
        List<Tag> tagList = question.getTagList();
        //判断是否有标签
        if(tagList != null && tagList.size()>0){
            for (Tag tag : tagList) {
                Map map = new HashMap<>();
                map.put("questionId",question.getId());
                map.put("tagId",tag.getId());
                System.out.println(map);
                questionBasicDao.addQuestionTag(map);
            }
        }
    }
}
